<template>
  <div class="daily-plan" v-loading="loading">
    <el-row
      type="flex"
      justify="space-between"
      align="middle"
      class="pre-plan-btn mb20"
    >
      <div class="full-calendar-header">
        <div class="header-center">
          <span class="prev-month" @click.stop="goPrev"
            ><i class="el-icon-arrow-left"></i
          ></span>
          <span class="title">{{ title }}</span>
          <el-date-picker
            class="date-picker"
            size="small"
            v-model="currentMonth"
            type="date"
            format="yyyy-MM-dd"
            placeholder="选择日期"
            :clearable="false"
          >
          </el-date-picker>
          <span class="next-month" @click.stop="goNext"
            ><i class="el-icon-arrow-right"></i
          ></span>
        </div>
      </div>
      <div></div>
      <h3 style="margin:0;font-size:18px">细纱计划产量</h3>
      <el-row type="flex">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="addPlan"
          >新增计划</el-button
        >
        <el-button size="small">导出日计划</el-button>
      </el-row>
    </el-row>
    <div class="common-table">
      <div style="width: fit-content; min-width: calc(100% - 8px)">
        <!-- <xi-table addTable :tableData="addData" :time="this.search.time" /> -->
        <xi-table
          :tableData="tableData"
          :sumIndex="sumIndex"
          :areaArr="areaArr"
          :planner="planner"
          :pzOption="pzOption"
          :pzOptionQf="pzOptionQf"
          :time="search.time"
        />
      </div>
    </div>
    <add-dialog
      ref="addDialog"
      :pzOption="pzOption"
      :pzOptionQf="pzOptionQf"
      :time="search.time"
      @getData="getData"
    ></add-dialog>
  </div>
</template>

<script>
import moment from 'moment';
import { commonPz } from '@/api/common';
import XiTable from '../components/xitable.vue';
import { getDailyPlan } from '@/api/productionPlan';
import AddDialog from '../components/add.vue';
export default {
  components: { XiTable, AddDialog },
  data() {
    return {
      search: {
        gongxu: '细纱',
        time: ''
      },
      currentMonth: moment(),
      actualMonth: moment(),
      addData: [], //新增的表格
      tableData: [],
      areaArr: [], //表格行数组
      areaSpan: 0,
      sumIndex: 0,
      planner: '',
      loading: false,
      pzOption: [],
      pzOptionQf: []
    };
  },
  computed: {
    title() {
      if (!this.currentMonth) return;
      return moment(this.currentMonth)
        .locale('zh')
        .format('YYYY[年] M[月] DD[日]');
    },
    isCurrMonth() {
      let d1 = new Date(this.actualMonth._d).getTime();
      let d2 = new Date(this.currentMonth._d).getTime();
      return d1 == d2;
    }
  },
  watch: {
    currentMonth: function(val) {
      if (val) {
        this.search.time = moment(val).format('YYYY-MM-DD');
        this.getData();
      }
    }
  },
  created() {
    this.getPz();
    this.search.time = moment().format('YYYY-MM-DD');
    this.getData();
  },
  methods: {
    async getData() {
      this.loading = true;
      const res = await getDailyPlan(this.search);
      if (res.code == 1) {
        let { data } = res;
        let arr = data.planInfo;
        this.areaArr = []; //表格行数组
        this.areaSpan = 0;
        this.mergeTable(arr);
        this.sumIndex = arr.length;
        // if (data.qcSum) {
        //   arr.push(data.qcSum[0]);
        // }
        arr = arr.concat(data.qcSum);
        arr = arr.concat(data.cjSum);
        this.tableData = arr;
        this.planner = data.planner;
      }
      this.loading = false;
    },
    async getPz() {
      const res = await commonPz('细纱');
      if (res.code == 1) {
        this.pzOption = res.data;
      }
      const res1 = await commonPz('粗纱');
      if (res1.code == 1) {
        this.pzOptionQf = res1.data;
      }
    },
    // 合并表格
    mergeTable(data) {
      for (let i = 0; i < data.length; i++) {
        if (i == 0) {
          this.areaArr.push(1);
          this.areaSpan = 0;
        } else {
          if (data[i].cj == data[i - 1].cj) {
            this.areaArr[this.areaSpan] += 1;
            this.areaArr.push(0);
          } else {
            this.areaArr.push(1);
            this.areaSpan = i;
          }
        }
      }
    },
    addPlan() {
      this.$refs.addDialog.show('细纱');
    },
    goPrev() {
      var newMonth = moment(this.currentMonth).subtract(1, 'day');
      this.currentMonth = newMonth;
    },
    goNext() {
      var newMonth = moment(this.currentMonth).add(1, 'day');
      this.currentMonth = newMonth;
    },
    goCurrMonth() {
      this.currentMonth = this.actualMonth;
    }
  }
};
</script>

<style lang="scss" scoped>
.daily-plan {
  height: 100%;
}
.common-table {
  height: calc(100% - 52px);
  overflow: auto;
  border-right: 1px solid #ebeef5;
}
.header-center {
  position: relative;
  .date-picker {
    width: 138px;
    position: absolute;
    left: 34px;
    top: 0;
    opacity: 0;
    cursor: pointer;
    ::v-deep.el-input__inner {
      cursor: pointer;
    }
  }
}
</style>
